package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class SignOnServlet extends HttpServlet {
    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String SIGNONFORM = "/WEB-INF/jsp/account/SignonForm.jsp";


    private String username;
    private String password;
    private AccountService accountService;
    private Account account = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        accountService = new AccountService();

        username = request.getParameter("username");
        password = request.getParameter("password");
        account = new Account();
        account = accountService.getAccount(username, password);

        PrintWriter out = response.getWriter();
        String message;

        if (account != null) {
            HttpServletRequest httpRequest = request;
            session.setAttribute("account", account );
            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " 用户登录";
            logService.insertLogInfo(account.getUsername(), logInfo);

            message = "Log-in Succeed";
        }

//        //获得输入的验证码值
//        String value1=request.getParameter("vCode");
//        /*获取图片的值*/
//        String value2=(String)session.getAttribute("checkcode");
//        Boolean isSame = false;
//        /*对比两个值（字母不区分大小写）*/
//        if(value2.equalsIgnoreCase(value1)){
//            isSame = true;
//        }

//      || !isSame
        else {
//            if(!isSame){
//                request.setAttribute("messageSignOn", "Invalid Verification Code.   Signon failed.");
//            }else{
            request.setAttribute("messageSignOn", "Invalid username or password.  Signon failed.");
            //?
            session.setAttribute("account", null);
            message = "Log-in Failed";
            request.getRequestDispatcher(SIGNONFORM).forward(request, response);
        }
//        else{
//            session.setAttribute("account",account);
//            session.setAttribute("username",username);
//            session.setAttribute("password",password);
//            request.getRequestDispatcher(MAIN).forward(request, response);
//        }
        out.print(message);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
